package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.infrastructure.entity.po.TravelFriendRelationPO;
import com.todo.strategy.online.infrastructure.mapper.TravelFriendRelationMapper;
import com.todo.strategy.online.infrastructure.repository.TravelFriendRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TravelFriendRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("travelFriendRelationRepository")
public class TravelFriendRelationRepositoryImpl implements TravelFriendRelationRepository {
    @Resource
    private TravelFriendRelationMapper travelFriendRelationMapper;


    @Override
    public Boolean updateTravelFriendRelationPOById(TravelFriendRelationPO travelFriendRelationPO) {
        if (travelFriendRelationPO == null || travelFriendRelationPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!backFieldCheck(travelFriendRelationPO)) {
            return false;
        }
        return travelFriendRelationMapper.updateTravelFriendRelationPOById(travelFriendRelationPO) > 0;
    }

    @Override
    public Boolean removeTravelFriendRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return travelFriendRelationMapper.removeTravelFriendRelationPOById(id) > 0;
    }

    @Override
    public TravelFriendRelationPO queryTravelFriendRelationPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return travelFriendRelationMapper.queryTravelFriendRelationPOById(id);
    }

    @Override
    public List<TravelFriendRelationPO> queryTravelFriendRelationListByPlanId(Long planId) {
        if (planId == null || planId < 0) {
            return Lists.newArrayList();
        }
        return travelFriendRelationMapper.queryTravelFriendRelationListByPlanId(planId);
    }

    @Override
    public List<TravelFriendRelationPO> queryJoinTravelRelationListByFriendId(Long friendId) {
        if (friendId == null || friendId < 0) {
            return Lists.newArrayList();
        }
        return travelFriendRelationMapper.queryJoinTravelRelationListByFriendId(friendId);
    }

    @Override
    public Boolean insertTravelFriendRelationPO(TravelFriendRelationPO travelFriendRelationPO) {
        if (travelFriendRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!backFieldCheck(travelFriendRelationPO)) {
            return false;
        }
        return travelFriendRelationMapper.insertTravelFriendRelationPO(travelFriendRelationPO) > 0;
    }

    private boolean backFieldCheck(TravelFriendRelationPO travelFriendRelationPO) {
        if (travelFriendRelationPO == null) {
            return false;
        }
        if (travelFriendRelationPO.getPlanId() == null || travelFriendRelationPO.getStrategyId() == null || travelFriendRelationPO.getFriendId() == null) {
            log.warn("TravelFriendRelationRepository#backFieldCheck 核心字段校验失败，操作失败 travelFriendRelationPO：【{}】", JSONObject.toJSONString(travelFriendRelationPO));
            return false;
        }
        if (travelFriendRelationPO.getConfirm() == null) {
            log.warn("TravelFriendRelationRepository#backFieldCheck 基础字段校验失败，操作失败 travelFriendRelationPO：【{}】", JSONObject.toJSONString(travelFriendRelationPO));
            return false;
        }
        return true;
    }
}
